"""
Case Type   : 功能测试
Case Name   : 初始用户给自己赋予proxy权限
Create At   : 2024-11-29
Owner       : wan005
Description :
        1.初始用户给自己赋予proxy权限
        2.初始用户回收自己的权限
Expect      :
        1.赋权成功
        2.权限回收成功
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_revoke(self):
        text = '-----step1:初始用户给自己赋予proxy权限;expect:创建成功-----'
        self.log.info(text)
        sql1 = f'grant proxy on {self.user_node.ssh_user} to ' \
               f'{self.user_node.ssh_user} with grant option;'
        res1 = self.pri_sh.execut_db_sql(sql1,
                                sql_type=f'-U {self.user_node.ssh_user}')
        self.log.info(res1)
        self.assertIn(f'the role granted is the same as the role of grantee: '
                      f'"{self.user_node.ssh_user}"', res1, f'执行失败: {text}')
        self.assertIn('GRANT ROLE', res1, f'执行失败: {text}')

        text = '----step2:初始用户回收自己的权限;expect:成功----'
        self.log.info(text)
        sql2 = f'revoke proxy on {self.user_node.ssh_user} from ' \
               f'{self.user_node.ssh_user};'
        res2 = self.pri_sh.execut_db_sql(sql2,
                                sql_type=f'-U {self.user_node.ssh_user}')
        self.log.info(res2)
        self.assertIn('REVOKE ROLE', res2, f'执行失败: {text}')

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')